//
//  PayListViewController.m
//  quickBatchManager
//
//  Created by jack on 15/10/15.
//  Copyright © 2015年 jack. All rights reserved.
//

#import "PayListViewController.h"

@interface PayListViewController ()<AddPayDelegate>
{
    NSMutableArray *array;
}
@end

@implementation PayListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    
}


-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
  
}


-(void)initData
{
    LKDBHelper *helper  = [LKDBHelper getUsingLKDBHelper];
    array = [helper search:[PayModel class] where:nil orderBy:nil offset:0 count:0];
}

-(void)initUI
{
    UIImage *image = _imageView.image;
    image = [image stretchableImageWithLeftCapWidth:floorf(image.size.width/2) topCapHeight:floorf(image.size.height/2)];
    _imageView.image=image;
    
    self.view.backgroundColor = [UIColor clearColor];
    
    [_addBtn bk_whenTapped:^{
        AddPayViewController *addPayVC = [[AddPayViewController alloc] init];
        addPayVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        addPayVC.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
        self.definesPresentationContext = YES;
        addPayVC.delegate = self;
        [self presentViewController:addPayVC animated:YES completion:nil];
    }];
    
    [_closeBtn bk_whenTapped:^{
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(array!=nil)
        return [array count];
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSString *reuseIdetify = @"PayListTableViewCell";
    PayListTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdetify];
    if(!cell){
        
        
        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"PayListTableViewCell" owner:self options:nil];
        for(id oneObject in nib){
            if([oneObject isKindOfClass:[PayListTableViewCell class]]){
                cell = (PayListTableViewCell *) oneObject;
            }
        }
        
    }
    PayModel *model = [array objectAtIndex:indexPath.row];
    
    cell.dateLabel.text = model.date;
    cell.moneyLabel.text = [NSString stringWithFormat:@"%0.2f",model.money];
    cell.remarkLabel.text = model.remark;
    
    LKDBHelper *helper  = [LKDBHelper getUsingLKDBHelper];
    PayTypeModel *type = [helper searchSingle:[PayTypeModel class] where:[NSString stringWithFormat:@"t_id=%ld",(long)model.payTypeId] orderBy:nil];
    cell.fenleiLabel.text = type.payTypeName;
    
    cell.tag = indexPath.row;
    NSMutableArray *rightUtilityButtons = [NSMutableArray new];
    [rightUtilityButtons sw_addUtilityButtonWithColor:[UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f] title:@"删除"];
    [cell setRightUtilityButtons:rightUtilityButtons];
    cell.delegate = self;
   
    return cell;

}

-(void) AddPayRefresh:(id)sender
{
    LKDBHelper *helper  = [LKDBHelper getUsingLKDBHelper];
    array = [helper search:[PayModel class] where:nil orderBy:nil offset:0 count:0];
    [_mTableView reloadData];
}


- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index
{
    PayModel *model = [array objectAtIndex:cell.tag];
    [model deleteToDB];
    [self initData];
    [self.mTableView reloadData];
    
    
}
@end
